import socket
import _thread
import time

addr_set = set()

def server(s=socket.socket()):
    while True:
        data, addr = s.recvfrom(1024)
        print('Receive({}):{}'.format(data.decode(), addr))
        addr_str = '{}:{}'.format(addr[0], addr[1])
        if addr_str not in addr_set:
            addr_set.add(addr_str)
            s.sendto('{}$&{}'.format('firstResponse', addr_str).encode(), addr)
        tmp_addr_list = list(addr_set)
        tmp_addr_list.remove(addr_str)
        s.sendto('{}$&{}'.format('addrs',tmp_addr_list.__str__()).encode(),
                 addr)

if __name__ == '__main__':
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    ip = '175.24.109.191'
    s.bind(('0.0.0.0', 8000))
    _thread.start_new_thread(server, (s,))
